1 00:00:00,770 --> 00:00:01,520 Welcome. 2 00:00:01,520 --> 00:00:07,130 In this lecture, we're going to disable player collisions between players by having all the descendant 3 00:00:07,130 --> 00:00:11,270 based parts in a player's character be set to the player's collision group. 4 00:00:11,270 --> 00:00:15,920 So if you remember when we first set up our game, if we go to the model tab and we open up collision 5 00:00:15,920 --> 00:00:20,690 groups, we created a few collision groups and one of them was a player's collision group. 6 00:00:20,690 --> 00:00:24,290 And this player's collision group cannot collide with other players. 7 00:00:24,290 --> 00:00:28,910 So that means we need to go through all of the limbs inside of our player's character, set them to 8 00:00:28,910 --> 00:00:32,870 this collision group to prevent other players from colliding with each other. 9 00:00:33,960 --> 00:00:39,150 So to get started, we can go ahead and create a new script inside of our server script service and 10 00:00:39,150 --> 00:00:42,630 we can call this our player collision handler. 11 00:00:44,170 --> 00:00:50,920 We'll use the same template as before, and we're going to go ahead and grab the player service. 12 00:00:54,610 --> 00:00:59,440 And from here we can go ahead and listen for when a player joins our game. 13 00:01:01,660 --> 00:01:06,820 It's because we want to listen to when a player has a character added to them, and we can go ahead 14 00:01:06,820 --> 00:01:11,530 and connect a function or create a dedicated function for when a character is added. 15 00:01:11,530 --> 00:01:18,490 We can call this on character added and get that character, and we'll connect on character added to 16 00:01:18,490 --> 00:01:21,310 this player's character added event. 17 00:01:21,310 --> 00:01:27,010 You could create a lambda function here instead, but that means you'll be creating this lambda function 18 00:01:27,010 --> 00:01:29,170 for every single player that joins your game. 19 00:01:29,170 --> 00:01:34,570 So if you want to be more memory efficient, instead we can create a single function outside and then 20 00:01:34,570 --> 00:01:39,760 refer or just connect the single function to every single player that has a character added. 21 00:01:39,970 --> 00:01:44,650 Then what we would want to do when a player has a character added is we want to loop through all of 22 00:01:44,650 --> 00:01:50,050 the descendants inside of that character and find all the bass parts, set them to the correct collision 23 00:01:50,050 --> 00:01:55,360 group, and then we also want to listen for when a descendant is added or removed from our character 24 00:01:55,360 --> 00:02:00,070 model, and if that descendant that's being added or removed is a bass part, then we want to go ahead 25 00:02:00,070 --> 00:02:03,070 and update the collision group for that part as well. 26 00:02:03,070 --> 00:02:06,280 So let's go ahead and create a couple more functions. 27 00:02:06,520 --> 00:02:11,920 One function we'll have is we'll listen for when a descendant is added to our character. 28 00:02:11,920 --> 00:02:13,270 And we'll get that descendant. 29 00:02:14,010 --> 00:02:19,080 And then we also want have a function to listen for when a descendant is removed. 30 00:02:19,470 --> 00:02:21,480 And we'll get that descendant as well. 31 00:02:21,720 --> 00:02:28,590 So from this point, what we can go ahead and do is listen to our character dot descendant added event 32 00:02:28,590 --> 00:02:31,890 and we'll connect our on descendant added function. 33 00:02:32,580 --> 00:02:39,630 And then for our character dot descendant removing event will also connect our on a descendant removed 34 00:02:39,630 --> 00:02:40,440 function. 35 00:02:41,670 --> 00:02:43,560 And these are going to be very simple functions. 36 00:02:43,560 --> 00:02:47,040 We're just going to check if this descendant is a base part. 37 00:02:47,040 --> 00:02:54,810 So if this descendant is not a base part so is a base part then we're just going to return. 38 00:02:54,810 --> 00:03:01,380 Otherwise we can go ahead and update this descendants collision group property to be equal to. 39 00:03:02,240 --> 00:03:08,030 And since this descendant is being removed, we want to update it to the default collision group, which 40 00:03:08,030 --> 00:03:09,680 is just the string of default. 41 00:03:09,710 --> 00:03:11,720 Otherwise, we can copy this. 42 00:03:11,720 --> 00:03:16,820 And when a descendant is added to a player's character, we want to set the collision group to the player's 43 00:03:16,820 --> 00:03:17,720 collision group. 44 00:03:18,630 --> 00:03:24,690 Now we can just loop through every single part inside of our character, and we're going to use the 45 00:03:24,690 --> 00:03:28,620 Get descendants function. 46 00:03:29,850 --> 00:03:35,130 And we can basically do exactly what we did for the descendant added event. 47 00:03:35,130 --> 00:03:40,440 But instead of copying this and pasting that there, we could literally just call our on descendant 48 00:03:40,440 --> 00:03:44,190 added function and just pass the part from this for loop. 49 00:03:44,310 --> 00:03:49,380 So now every single player in our game, when they have a character added, we're going to force all 50 00:03:49,380 --> 00:03:52,230 of the base parts to have the player's collision group. 51 00:03:52,230 --> 00:03:57,210 And then when a descendant is removed from our player's character, then we're going to place it back 52 00:03:57,210 --> 00:03:58,980 in the default collision group. 53 00:04:00,150 --> 00:04:04,680 And I've placed these functions in the wrong section for my comments. 54 00:04:04,680 --> 00:04:09,030 There is no public functions here, so I'm just going to delete that and put it there. 55 00:04:09,030 --> 00:04:14,190 And now we should have our collision groups completely set up for our players. 56 00:04:14,190 --> 00:04:20,850 So to completely test this out, we can go ahead and set up a two player server and see if our players 57 00:04:20,850 --> 00:04:21,990 collide with each other. 58 00:04:23,160 --> 00:04:29,190 So here I have both of my players in the game, and if I go and walk towards this player, as you can 59 00:04:29,190 --> 00:04:35,340 see, I pass right through them because both of these players have the players collision group and we 60 00:04:35,340 --> 00:04:37,230 can no longer collide with each other. 61 00:04:37,230 --> 00:04:43,200 This prevents players from being able to stack on top of each other and trying to escape the map, and 62 00:04:43,200 --> 00:04:47,340 it also prevents just players from colliding with other players in general. 63 00:04:48,220 --> 00:04:53,020 I hope you're enjoying the project so far and I'll see you in the next lecture.